//
//  MultiVistaViewController.m
//  MultiVista
//
//  Created by Marcelo Perretta on 19/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MultiVistaViewController.h"
#import "AViewController.h"
#import "BViewController.h"

@implementation MultiVistaViewController

@synthesize controladorVistaA = _controladorVistaA;
@synthesize controladorVistaB = _controladorVistaB;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    AViewController *aController = [[AViewController alloc] initWithNibName:@"AView" bundle:nil];
    self.controladorVistaA = aController;
    [self.view addSubview:aController.view];
    [aController release];

}

- (IBAction) intercambiarVistas: (id) sender
{
	[UIView beginAnimations:@"View Flip" context:nil];
	[UIView setAnimationDuration:1.0];
	[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
	
	if (self.controladorVistaB.view.superview == nil)
	{
		// estábamos en al vista A luego tenemos que mostrar la B
		if (self.controladorVistaB == nil)
		{
			// Si la B nunca se ha creado, o ha sido borrada de memoria
			BViewController *bController = [[BViewController alloc] initWithNibName:@"BView" bundle:nil];
			self.controladorVistaB = bController;
			[bController release];
		}
		
		[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
		
		[self.controladorVistaA viewWillDisappear:YES];
		[self.controladorVistaB viewWillAppear:YES];
		
		[self.controladorVistaA.view removeFromSuperview];
		[self.view insertSubview:_controladorVistaB.view	atIndex:0];
		
		[self.controladorVistaA viewDidDisappear:YES];
		[self.controladorVistaB viewDidAppear:YES];
	}
	else
	{
		// estábamos en al vista B luego tenemos que mostrar la A
		if (self.controladorVistaA == nil)
		{
			// Si la A nunca se ha creado, o ha sido borrada de memoria
			AViewController *aController = [[AViewController alloc] initWithNibName:@"AView" bundle:nil];
			self.controladorVistaA = aController;
			[aController release];
		}
		
		[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
		
		[self.controladorVistaB viewWillDisappear:YES];
		[self.controladorVistaA viewWillAppear:YES];
		
		[self.controladorVistaB.view removeFromSuperview];
		[self.view insertSubview:_controladorVistaA.view	atIndex:0];
		
		[self.controladorVistaB viewDidDisappear:YES];
		[self.controladorVistaA viewDidAppear:YES];
	}
	
	[UIView commitAnimations];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) dealloc{
    
    [_controladorVistaA release];
    [_controladorVistaB release];
    [super release];
}

@end
